import datetime
import time
from asyncio import sleep

from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

# # 编写自动化脚本具体步骤
# # 1. 打开浏览器 --- 驱动管理
# ChromIns = ChromeDriverManager().install()
# ### 创建谷歌浏览器驱动对象
# driver = webdriver.Chrome(service=Service(ChromIns))
# # 2. 输入百度网址: https://www.baidu.com(网址一定要完整)
# driver.get("https://www.baidu.com")
# time.sleep(2)
# # 3. 找到百度输入框，输入关键词”张家界学院“
# driver.find_element(By.CSS_SELECTOR,"#chat-textarea").send_keys("迪丽热巴")
# # 4. 点击”百度一下“按钮
# driver.find_element(By.CSS_SELECTOR,"#chat-submit-button").click()
# time.sleep(2)
# # 5. 关闭浏览器
# driver.quit()

# ChromIns = ChromeDriverManager().install()
# driver = webdriver.Chrome(service=Service(ChromIns))
# driver.get("https://www.baidu.com")
# ret = driver.find_elements(By.CSS_SELECTOR, "#hotsearch-content-wrapper > li")
# for i in ret:
#     print(i.text)
# driver.quit()

# ChromIns = ChromeDriverManager().install()
# # 浏览器配置参数
# options = webdriver.ChromeOptions()
# # 无头模式 --- 直接在后台运行
# # options.add_argument('--headless')
# # options.page_load_strategy = 'normal' # 默认值，等待所有资源下载
# # options.page_load_strategy = 'eager' # DOM访问已就绪，但是图像和其他资源可能仍在加载
# options.page_load_strategy = 'none' # 完全不会阻塞 WebDriver
#
# driver = webdriver.Chrome(service=Service(ChromIns), options=options)
#
# driver.get("https://www.baidu.com")
# title = driver.title
# print(title)

# driver.get("https://image.baidu.com/")

# ret = driver.find_elements(By.CSS_SELECTOR, "#hotsearch-content-wrapper > li")
# for i in ret:
#     print(i.text)

# driver.find_element(By.CSS_SELECTOR,"#chat-submit-button").click()
# time.sleep(1)

# XPATH来选择选取内容
# text = driver.find_element(By.XPATH, '//*[@id="hotsearch-content-wrapper"]/li[2]/a/span[2]').text
# assert text == "张家界今年前7月乡镇接待费仅2100元"
# print(text)

# 模拟输入
# driver.find_element(By.CSS_SELECTOR, "#chat-textarea").send_keys("张家界学院")
# driver.find_element(By.CSS_SELECTOR, "#chat-textarea").clear()
# driver.find_element(By.CSS_SELECTOR, "#chat-textarea").send_keys("我是学生")
# time.sleep(2)

# 获取百度一下链接上的文字
# text = driver.find_element(By.CSS_SELECTOR, "#su").get_attribute("value")
# print(text)
# title = driver.title
# url = driver.current_url
# print(title)
# print(url)

# 页面切换
# print("before: " + driver.title)
# print("before: " + driver.current_url)

# driver.find_element(By.CSS_SELECTOR, "#s-top-left > a:nth-child(6)").click()
# time.sleep(3)

# driver.find_element(By.CSS_SELECTOR, "#s-top-left > a:nth-child(2)").click()

# # 获取当前页面的句柄 --- 第一个标签页
# curHandle = driver.current_window_handle
# # 获取所有页面
# allHandles = driver.window_handles
# for handle in allHandles:
#     if handle != curHandle:
#         # 页面切换
#         driver.switch_to.window(handle)

# 如果页面在当前页面变化就可以不需要这一步

# print("after: " + driver.title)
# print("after: " + driver.current_url)

# 设置窗口大小
# 最大化
# time.sleep(2)
# driver.maximize_window()
# # 最小化
# time.sleep(2)
# driver.minimize_window()
# # 全屏
# time.sleep(2)
# driver.fullscreen_window()
# # 手动设置尺寸
# time.sleep(2)
# driver.set_window_size(1024, 800)
# time.sleep(2)

# 屏幕截图
# filename = "./images/test.png"
# filename = "autotest-" + datetime.datetime.now().strftime("%Y-%m-%d-%H%M%S") + ".png"
# driver.save_screenshot("./images/" + filename)

# 页面关闭
# driver.find_element(By.CSS_SELECTOR, "#s-top-left > a:nth-child(6)").click()
# time.sleep(3)
# close()只会关闭当前页面，也就是第一次打开的页面
# driver.close()
# time.sleep(3)

# driver.find_element(By.CSS_SELECTOR, "#chat-textarea").send_keys("迪丽热巴")
# cli = driver.find_element(By.CSS_SELECTOR, "#chat-button")
# cli.click()
# driver.find_element(By.CSS_SELECTOR, "#\31  > div > div > div > div > div > div.new-tag_4ozgi.new-text-link_3k9GD > div > div.flex-wrapper-top_3ucFS > div.flex-col-left_3trtY.baike-wrapper_6AORN.cu-pt-xs-lg.baike-wrapper-pc_26R04.cu-pt-xl.baike-wrapper-left-pc_5eYY8.cos-space-pb-sm > div > div > p > span:nth-child(1) > span")

# driver.implicitly_wait(5)
# driver.find_element(By.CSS_SELECTOR, "#chat-textarea").send_keys("迪丽热巴")
# driver.find_element(By.CSS_SELECTOR, "#chat-submit-butto").click()
# time.sleep(3)
#
# # wait = WebDriverWait(driver, 10)
# # element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#chat-submit-button")))
# # element.click()
# # time.sleep(3)
#
# driver.find_element(By.CSS_SELECTOR, "#\\31  > div > div > div > div > div > div.new-tag_4ozgi.new-text-link_3k9GD > div > div.flex-wrapper-top_3ucFS > div.flex-col-left_3trtY.baike-wrapper_6AORN.cu-pt-xs-lg.baike-wrapper-pc_26R04.cu-pt-xl.baike-wrapper-left-pc_5eYY8.cos-space-pb-sm > div > div > p > span:nth-child(1) > span")

# driver.get("file:///C:/Users/taogeliubi/AppData/Local/Temp/7439afea-8468-49d4-814b-6f820b5af0af_%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E8%AF%BE%E4%BB%B6.zip.0af/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E8%AF%BE%E4%BB%B6/selenium-html/alert.html")
# driver.get("file:///C:/Users/taogeliubi/AppData/Local/Temp/c55ac8ab-38ff-408c-9fbd-3764cd1315d0_%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E8%AF%BE%E4%BB%B6.zip.5d0/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E8%AF%BE%E4%BB%B6/selenium-html/Prompt.html")
#
# driver.find_element(By.CSS_SELECTOR, "body > input[type=button]").click()
# time.sleep(2)
#
# alert = driver.switch_to.alert
#
# alert.send_keys("Auto test")
#
# alert.accept()
# # alert.dismiss()
# time.sleep(2)

# 浏览器操作
# driver.get("https://www.baidu.com/")
# driver.back()
# time.sleep(3)
# driver.forward()
# time.sleep(3)
# driver.refresh()
# time.sleep(3)

# 文件上传
# driver.get("file:///C:/Users/taogeliubi/AppData/Local/Temp/c3bf946d-c697-40bd-bc1c-e7646d15b4f8_%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E8%AF%BE%E4%BB%B6.zip.4f8/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E8%AF%BE%E4%BB%B6/selenium-html/upload.html")
# time.sleep(2)
# driver.find_element(By.CSS_SELECTOR, "body > div > div > input[type=file]").send_keys("D:\\5EClient\\updater.log")
# time.sleep(2)

driver.quit()